#pragma once
#include <iostream>
#include<pthread.h>

class Mutex
{
public:
    Mutex(pthread_mutex_t *pmutex)
        : _pmutex(pmutex)
    {
    }

    void lock()//加锁
    {
        pthread_mutex_lock(_pmutex);
    }

    void unlock()//解锁
    {
        pthread_mutex_unlock(_pmutex);
    }

private:
    pthread_mutex_t *_pmutex;
};


class lockGuard
{
public:
    lockGuard(pthread_mutex_t* pmutex)//构造：在构造时加锁
    :_mutex(pmutex)//初始化列表初始化_mutex时，调用Mutex的构造函数，需要传入pthread_mutex_t * 类型变量
    {
        _mutex.lock();//上锁
    }

    ~lockGuard()//析构：在对类析构时，顺带就解锁
    {
        _mutex.unlock();//解锁
    }
private:
    Mutex _mutex;
};